package com.arcvideo.acsconnect.app;

import android.content.pm.ApplicationInfo;
import android.support.multidex.MultiDexApplication;
import android.util.Log;

import com.arcvideo.acsconnect.helper.MessageHelper;
import com.arcvideo.acsconnect.rest.model.Account;
import com.arcvideo.acsconnect.rest.model.UserBean;
import com.arcvideo.rtcmessage.utils.LogUtil;
import com.arcvideo.tyingyitonginhouse.BuildConfig;
import com.framework.core.rest.HttpUtils;
import com.framework.core.utils.ToastUtil;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author shenmengchao
 * @version 1.0.0
 * @date 2017/10/17
 * @description
 */

public class AcsConnectApplication extends MultiDexApplication {

    private static String TAG = "AcsConnectApplication";

    private static String _strFileDir = null;
    private static String _strLibsDir = null;
    private static boolean _bOuputLog = true;

    private static AcsConnectApplication mInstance;

    public static AcsConnectApplication getInstance() {
        return mInstance;
    }

    public static IWXAPI mWxApi;
    public static List<UserBean> sUserList = new ArrayList<>();
    public static List<Account> sNurseList = new ArrayList<>();
    public static int sCurrentUserIndex = 0;
//    public static boolean sIsLoginFromBoot = false;

    public static UserBean getCurrentUserBean() {
        if (sUserList.size() > sCurrentUserIndex) {
            return sUserList.get(sCurrentUserIndex);
        }
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        HttpUtils.getInstance().setBaseUrl(BuildConfig.HTTP_HOST).init();
        ToastUtil.setApplicationContext(this);
        MessageHelper.setApplicationContext(this);
        copyPlginIni();
        loadLibrary();
        registToWX();
        initBugly();
//        RtcMessageClient.getInstance()
//                .setBuildType(BuildConfig.BUILD_TYPE)
//                .setPackageName(AppUtil.getPackageName(this))
//                .setAppName("AcsConnect")
//                .init();

        //网易云信初始化 options==null表示采用默认配置
        LogUtil.init(this);
    }

    private void initBugly() {
        CrashReport.initCrashReport(getApplicationContext(), "4034d4f190", true);
    }

    private void registToWX() {
        //AppConst.WEIXIN.APP_ID是指你应用在微信开放平台上的AppID，记得替换。
        mWxApi = WXAPIFactory.createWXAPI(this, GlobalConstant.APP_ID_WEIXIN, false);
        // 将该app注册到微信
        mWxApi.registerApp(GlobalConstant.APP_ID_WEIXIN);
    }

    private void copyPlginIni() {
        try {
            getCurAppDir();
            String strINIFile = _strFileDir;

            File dirFile = new File(strINIFile);
            if (!dirFile.exists()) {
                if (!dirFile.mkdirs()) {
                    return;
                }
            }
            File fileExist = new File(strINIFile + "ArcPlugin.ini");
            if (fileExist.exists()) {
                fileExist.delete();
            }

            InputStream is = getBaseContext().getAssets().open("ArcPlugin.ini");
            File file = new File(strINIFile + "ArcPlugin.ini");
            if (!file.exists()) {
                byte[] buf = new byte[1024];
                file.createNewFile();
                FileOutputStream os = new FileOutputStream(file);
                while (true) {
                    int ins = is.read(buf);
                    if (ins > 0) {
                        os.write(buf, 0, ins);
                    } else {
                        is.close();
                        os.flush();
                        os.close();
                        break;
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void outputLog(String strTAG, String strInfo) {
        if (_bOuputLog)
            Log.i(strTAG, strInfo);
    }

    private String getCurAppDir() {
        ApplicationInfo applicationInfo = getApplicationInfo();
        outputLog(TAG, "cur data dir:" + this.getApplicationInfo().dataDir);
        outputLog(TAG, "cur file dir:" + getBaseContext().getFilesDir().getAbsolutePath());

        _strLibsDir = applicationInfo.dataDir;
        _strFileDir = getBaseContext().getFilesDir().getAbsolutePath();

        if (!_strLibsDir.endsWith("/")) {
            _strLibsDir = _strLibsDir + "/";
        }
        _strLibsDir = _strLibsDir + "lib/";
        outputLog(TAG, "cur libs dir:" + _strLibsDir);

        if (!_strFileDir.endsWith("/"))
            _strFileDir = _strFileDir + "/";

        if (!_strFileDir.endsWith("/"))
            _strFileDir = _strFileDir + "/";

        outputLog(TAG, "cur file dir:" + _strFileDir);
        return this.getApplicationInfo().dataDir;
    }

    private void loadLibrary() {
        System.loadLibrary("stlport_shared");
        System.loadLibrary("mv3_rtcmessagejni");
        System.loadLibrary("mv3_platform");
        System.loadLibrary("mv3_common");
        System.loadLibrary("rtp_rtcp");
        System.loadLibrary("mv3_camera");
        System.loadLibrary("mv3_mediarecorder");
        System.loadLibrary("mv3_mediarecorderjni");
        System.loadLibrary("mv3_mediapublisher");
        System.loadLibrary("mv3_rtcmediarecorderjni");
        System.loadLibrary("mv3_cameravideowriter");
//        System.loadLibrary("mv3_rtcjni");
    }
}
